<template>
    <div>
        <el-dialog center class="global-dialog" v-model="dialogVisable" width="800px" top="10vh" :show-close="true"
            :close-on-click-modal="false" @close="close">
            <template #title>
                <div class="my-header">
                    <el-tabs v-model="tabId" class="demo-tabs">
                        <el-tab-pane label="基本信息" name="1">
                            <el-form :model="diaForm" ref="DiaForm" :rules="rules" status-icon label-width="110px" status>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="软件名称" prop="name">
                                            <el-input placeholder="请输入" v-model="diaForm.name" style="width: 100%" />
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="首次发表日期" prop="firstPublicDate">
                                            <el-date-picker v-model="diaForm.firstPublicDate" type="date"
                                                value-format="YYYY-MM-DD" placeholder="选择日期" clearable
                                                style="width: 100%" />
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="登记号" prop="registerNo">
                                            <el-input placeholder="请输入" v-model="diaForm.registerNo" style="width: 100%" />
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="维护单位" prop="maintainUnitId">
                                            <el-select clearable v-model="diaForm.maintainUnitId" placeholder="维护单位"
                                                @change="changeUnit" style="width: 100%">
                                                <el-option :label="item.belongingUnit" :value="item.belongingCode"
                                                    :key="item.belongingCode" v-for="item in unitNameList" />
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="著作权人" prop="copyOwnerId">
                                            <el-select v-model="diaForm.copyOwnerId" placeholder="请选择" style="width: 100%"
                                                @change="changeWorker">
                                                <el-option :label="item.username" :value="item.userId" :key="item.userId"
                                                    v-for="item in workerList" />
                                            </el-select>

                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="发证日期" prop="issueDate">
                                            <el-date-picker v-model="diaForm.issueDate" type="date"
                                                value-format="YYYY-MM-DD" placeholder="选择日期" clearable
                                                style="width: 100%" />
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="所属项目" prop="projectId">
                                            <el-select v-model="diaForm.projectId" placeholder="请选择" style="width: 100%"
                                                @change="changeProgect">
                                                <el-option :label="item.projectname" :value="item.id" :key="item.id"
                                                    v-for="item in projectList" />
                                            </el-select>

                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="技术分类" prop="techClassId">
                                            <el-select v-model="diaForm.techClassId" placeholder="请选择" style="width: 100%"
                                                @change="changeType">
                                                <el-option :label="el.itemText" :value="el.itemValue" :key="k"
                                                    v-for="(el, k) in technologyList" />
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="软著状态" prop="softStateId">
                                            <el-select v-model="diaForm.softStateId" placeholder="请选择" style="width: 100%"
                                                @change="changeState">
                                                <el-option :label="el.itemText" :value="el.itemValue" :key="k"
                                                    v-for="(el, k) in awardList" />
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-form-item label="证书存放地" prop="saveAddress">

                                    <el-input v-model="diaForm.saveAddress" :autosize="{ minRows: 2, maxRows: 6 }"
                                        type="textarea" placeholder="请输入" />
                                </el-form-item>
                                <el-form-item label="备注" prop="remark">
                                    <el-input v-model="diaForm.remark" :autosize="{ minRows: 2, maxRows: 6 }"
                                        type="textarea" placeholder="请输入" />
                                </el-form-item>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="相关附件" name="2">
                            <PlatformFiles :files="files" @filesParams="filesParams"></PlatformFiles>
                        </el-tab-pane>
                        <el-tab-pane label="变更记录" name="3" v-if="id">
                            <Record :changeLogs="changeLogs"></Record>
                        </el-tab-pane>
                    </el-tabs>
                </div>
            </template>

            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="close">关闭</el-button>
                    <el-button type="primary" @click="save">保存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
  
<script >
import {
addForm,
editForm,
getDetailById,
getDictionary,
getProject,
getUnit, getUser
} from "@/views/science/api/achievements";
import PlatformFiles from '@/views/science/components/platformFiles/platformFiles.vue';
import Record from '@/views/science/components/record/record.vue';
import { tips } from "@/views/science/components/tips/tips";
import { onMounted, reactive, ref, toRefs, watch } from "vue";


export default {
    components: {
        PlatformFiles, Record
    },
    props: {
        dialogVisable: {
            type: Boolean,
            default: false,
        },

        // falg  1:新增  2:编辑  3:详情
        flag: {
            type: String,
            default: 1,
        },
        id: {
            type: String,
            default: '',
        }
    },
    setup(props, { emit }) {


        watch(
            () => props.id,

            () => {

                // 深度监听.只有在id存在的时候调用详情的接口
                if (props.id) {
                    getDetail(props.id)
                }
            },

            { deep: true, immediate: true }
        );
        // 发送请求
        onMounted(() => {
            // 获取数据字典
            getUnitList();

            getWorkList()

            getProjectList()

            getCodeList()

        });

        let DiaForm = ref();
        let data = reactive({
            tabId: '1',
            unitNameList: [],//维护单位
            workerList: [],//桌著作人
            projectList: [],//项目
            technologyList: [],//技术分类
            awardList: [],//软著状态

            diaForm: {
                id: '',
                name: '',
                firstPublicDate: "",
                registerNo: '',
                maintainUnitId: '',
                maintainUnit: '',
                copyOwnerId: '',
                copyOwner: '',
                issueDate: '',
                projectId: '',
                project: '',
                projectleader: '',
                techClassId: '',
                techClass: '',
                softState: '',
                softStateId: '',
                saveAddress: '',
                remark: '',
            },

            files: [],

            changeLogs: []
        });

        // 规则校验
        let rules = reactive({
            name: [
                { required: true, message: '请输入软件名称', trigger: 'blur' },
            ],
            firstPublicDate: [
                { required: true, message: '请选择首次发表日期', trigger: 'change', },
            ],
            maintainUnitId: [
                { required: true, message: '请选择维护单位', trigger: 'change', },
            ],
            copyOwnerId: [
                { required: true, message: '请选择著作权人', trigger: 'change', },
            ],
            issueDate: [
                { required: true, message: '请选择发证日期', trigger: 'change', },
            ],
            projectId: [
                { required: true, message: '请选择所属项目', trigger: 'change', },
            ],
            techClassId: [
                { required: true, message: '请选择技术分类', trigger: 'change', },
            ],
            softStateId: [
                { required: true, message: '请选择软著状态', trigger: 'change' },
            ]
        });
        // 保存
        let save = () => {


            DiaForm.value.validate((valid) => {

                // 校验通过
                if (valid) {
                    //flag 1 新增  2 编辑  3  详情
                    if (props.flag == 1) {

                        data.diaForm.id = '';

                        addForm(
                            {
                                info: data.diaForm,
                                files: data.files,
                            }
                        ).then(() => {

                            tips("新增成功", "success");

                            DiaForm.value.resetFields();

                            emit("changeDialog", '1');

                        });


                    } else {
                        editForm({
                            info: data.diaForm,
                            files: data.files,
                        }).then(() => {

                            tips("编辑成功", "success");

                            DiaForm.value.resetFields();

                            emit("changeDialog", '1');

                        });

                    }
                }else{

                    return tips('请将基本信息补充完整', 'warning')

                }


            });
        }


        let filesParams = (params) => {

            data.files = params
        }

        let changeState = (id) => {

            let itemText = data.awardList.find(item => item.itemValue == id).itemText

            data.diaForm.softState = itemText

        }

        let changeType = (id) => {


            let itemText = data.technologyList.find(item => item.itemValue == id).itemText

            data.diaForm.techClass = itemText

        }


        let changeUnit = (id) => {

            let itemText = data.unitNameList.find(item => item.belongingCode == id).belongingUnit

            data.diaForm.maintainUnit = itemText
        }

        let changeWorker = (id) => {

            let itemText = data.workerList.find(item => item.userId == id).username

            data.diaForm.copyOwner = itemText
        }


        let changeProgect = (id) => {

            let itemText = data.projectList.find(item => item.id == id).projectname

            data.diaForm.project = itemText
        }

        // 通过详情查询id

        let getDetail = async (id) => {

            let res = await getDetailById(id);

            data.diaForm = res.data.data.info

            data.files = res.data.data.files

            data.changeLogs = res.data.data.changeLogs


        };









        // 技术分类/软著状态===>数据字典

        let getCodeList = () => {

            getDictionary(['KJSL_BUSINESS_SECTOR', 'KJSL_SOFT_STATE']).then((res) => {

                data.technologyList = res.data.data.KJSL_BUSINESS_SECTOR;

                data.awardList = res.data.data.KJSL_SOFT_STATE;

            });
        };



        // 项目

        let getProjectList = () => {

            getProject({}).then((res) => {

                data.projectList = res.data.data;
            });
        };

        // 著作人

        let getWorkList = () => {

            getUser({}).then((res) => {

                data.workerList = res.data.data;
            });
        };

        // 单位
        let getUnitList = () => {

            getUnit({}).then((res) => {
                data.unitNameList = res.data.data;
            });
        };

        // 关闭清除对话框里面的数据,附件.变更记录也要删除

        let close = () => {

       

            data.tabId = '1';

            DiaForm.value.resetFields();

            data.files = [];

            data.changeLogs = [];

            emit("changeDialog");

         
        };

        return {
            DiaForm,
            filesParams,
            rules,
            changeState,
            changeType,
            changeUnit,
            changeProgect,
            changeWorker,
            getUnitList,
            getWorkList,
            getProjectList,
            getCodeList,
            ...toRefs(data),
            close,
            save
        };
    },
};
</script>
  
<style scoped lang="scss">
.dialog-footer {
    display: flex;
    justify-content: end;
}

:deep(.el-dialog__body) {
    display: none;
}

// 滚动条
:deep(.demo-tabs > .el-tabs__content) {
  max-height: 500px;
  overflow-y: scroll;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
</style>
  